package cn.luo.controller;

import cn.luo.entity.Blogs;
import cn.luo.entity.Content;
import cn.luo.entity.Tag;
import cn.luo.entity.User;
import cn.luo.service.*;
import cn.luo.uilt.RespJson;
import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.InputStream;
import java.lang.reflect.Method;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Controller
@RequestMapping("/api/public")
public class PublicController {
    @Autowired
    TagService tagService;

    @Autowired
    BlogsService blogsService;

    @Autowired
    ContentService contentService;

    @Autowired
    FileService fileService;


    @Autowired
    UserService userService;

    //进入主页
    @RequestMapping("/home")
    public String index(){
        return "/home";
    }

    //跳转登录页面
    @RequestMapping("/login")
    public String login(){
        return "public/login";
    }

    //跳转到注册
    @RequestMapping("/signin")
    public String signin(){
        return "public/signin";
    }

    //博文分页查询
    @ResponseBody
    @RequestMapping("/blogs/findByPage/{start}/{num}")
    public Map<String,Object> findByPage(@PathVariable("start")int start,
                                         @PathVariable("num")int num){
        System.out.println("start = " + start);
        System.out.println("num = " + num);
        Map<String,Object> map = new HashMap<>();
        map.put("blogs",blogsService.finfByPage(start,num));
        map.put("total",blogsService.total());
        System.out.println("map1 = " + map);
        return map;
    }

    //添加用户
    @ResponseBody
    @RequestMapping("/user/add")
    public Map<String, Object> addUser(User user,@RequestPart(value = "file", required = false) MultipartFile multipartFile){
        System.out.println("user = " + user);
        int i = userService.addUser(user, multipartFile);
        System.out.println("i =============== " + i);
        return RespJson.pack(200,"ok", i);
    }

    //访问博文详情页
    @RequestMapping("/blogs/details/{blogid}")
    public String details(@PathVariable("blogid") String id, Model model){
        Blogs blogs = blogsService.findByBlogsId(id);
        Content content = contentService.findById(blogs.getContentid());  //获取内容
        model.addAttribute("blog",blogs);
        model.addAttribute("content",content);
        System.out.println("id = " + id);
        return "public/details";
    }

    //查看全部类型
    @ResponseBody
    @RequestMapping("/tag/findAll")
    public List<Tag> fidAll(){
        List<Tag> lst = tagService.findAll();
        return lst;
    }

    //根据类型分页查询
    @ResponseBody
    @RequestMapping("/blogs/findByPageByTagId/{start}/{num}/{tagid}")
    public Map<String,Object> findByPageByTagId(@PathVariable("start")int start,
                                         @PathVariable("num")int num,
                                          @PathVariable("tagid")String tagid){
        System.out.println("start = " + start);
        System.out.println("num = " + num);
        System.out.println("tagid = " + tagid);
        Map<String,Object> map = new HashMap<>();
        map.put("blogs",blogsService.findByPageByTagId(start,num,tagid));
        map.put("total",blogsService.tagtotal(tagid));
        System.out.println("map = " + map);
        return RespJson.pack(200,"ok",map);
    }

    //显示图片文件
    @ResponseBody
    @RequestMapping("/showImg/{id}")
    public void showImg(@PathVariable("id") String id, HttpServletResponse response){
        System.out.println("showimgid = " + id);
        try{
            InputStream is = fileService.getInpuStreamByFilename(id);
            response.setContentType("image/img");
            IOUtils.copy(is, response.getOutputStream());
        }catch (Exception e) {
            System.out.println("e = " + e);
        }
    }
}
